var express = require('express');
var router = express.Router();
var mysql = require('mysql');

/* GET users listing. */
router.get('/', function(req, res, next) {

  var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'my_data'
  });

  connection.connect(function(err){
    if(err) {
      console.error("connect error:"+err.stack);
      return;
    }
    console.log("连接成功 id=" + connection.id);
  });

  connection.query("select name,sex from t_user", function(err, fields, result){
    var data = {
      title: '用户信息',
      users: fields,
      heads: ['序号','姓名','性别']
    }
    res.render("users", data);
    //res.send("callback arguments" + JSON.stringify(arguments));
  });

  // res.send('respond with a resource');
});

module.exports = router;
